package com.deparse.testapp;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.deparse.testapp.groupedgridview.GroupedGridView;
import com.deparse.testapp.groupedgridview.GroupedGridViewAdapter;
import com.deparse.testapp.gsondata.RecyclingManStockData;

public class MainActivity extends AppCompatActivity {

    GroupedGridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridView = (GroupedGridView) findViewById(R.id.gridview);

        String token = "0C1FFDACECBE7221F4F6AE0366358182";

        final String url = "http://bzb.dev.hsydev.com/api.php/Storage/recyclingManStock?user_name_re=17004100550&token=" + token;
        HttpUtil.init(this);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpUtil.httpGet(0, url, new HttpUtil.HttpCallback() {
                    @Override
                    public void onSuccess(int what, String response) {
                        Log.e("yyy", response);
                        RecyclingManStockData data = GsonUtil.fromJson(response, RecyclingManStockData.class);
                        gridView.setAdapter(new MyAdapter(MainActivity.this, data));
                    }

                    @Override
                    public void onError(int what, int type, VolleyError error) {
                        Log.e("yyy", error.toString());
                    }
                });
            }
        });

    }

    private class MyAdapter extends GroupedGridViewAdapter {

        private RecyclingManStockData data;
        private Context mContext;

        public MyAdapter(Context context, RecyclingManStockData data) {
            super(context);
            this.data = data;
            this.mContext = context;
        }

        @Override
        public int getGroupCount() {
            return data.getResult().getStorage().size();
        }

        @Override
        public Object getGroupItem(int position) {
            return data.getResult().getStorage().get(position);
        }

        @Override
        public long getGroupItemId(int position) {
            return Long.parseLong(data.getResult().getStorage().get(position).getId());
        }

        @Override
        public View getGroupView(int groupPosition, View convertView) {
            TextView textView = new TextView(mContext);
            textView.setText(data.getResult().getStorage().get(groupPosition).getName());
            return textView;
        }

        @Override
        public int getGridNumColumns(int groupPosition) {
            return 2;
        }

        @Override
        public int getGridCount(int groupPosition) {
            return data.getResult().getStorage().get(groupPosition).getChildren().size();
        }

        @Override
        public Object getGridItem(int groupPosition, int gridPosition) {
            return data.getResult().getStorage().get(groupPosition).getChildren().get(gridPosition);
        }

        @Override
        public long getGridItemId(int groupPosition, int gridPosition) {
            return Long.parseLong(data.getResult().getStorage().get(groupPosition).getChildren().get(gridPosition).getId());
        }

        @Override
        public View getGridItemView(int groupPosition, int gridPosition, View convertView) {
            TextView textView = new TextView(mContext);
            textView.setText(data.getResult().getStorage().get(groupPosition).getChildren().get(gridPosition).getCat_name());
            return textView;
        }

        @Override
        public void onGroupClick(int groupPosition, View v) {
            Toast.makeText(mContext, "clicked on group " + groupPosition, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onGridItemClick(int groupPosition, int gridPosition, View v) {
            Toast.makeText(mContext, "clicked on item (" + groupPosition + "," + gridPosition + ")", Toast.LENGTH_SHORT).show();
        }
    }
}
